package com.validateCodeYzm.zpf.service;

import com.github.kevinsawicki.http.HttpRequest;
import com.validateCodeYzm.zpf.lib.HttpRequestUtil;
import com.validateCodeYzm.zpf.lib.HttpResponseCollect;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Service8196 {

    //这个cookie可以放入到数据库 可以一定时间内多次使用  无需重复登录
    private LoginData loginData;

    public Service8196(LoginData loginData) {
        this.loginData = loginData;
    }

    private void setCookie(HttpRequest httpRequest){
        httpRequest.header("Cookie",loginData.getYzmCookie()+";"+loginData.getCookie8196());
    }

    public String toIndex(){
        return HttpRequestUtil.postWithMultipleInfo("https://202.100.182.103:8196/Index.aspx","GET",new HashMap<>(), this::setCookie)
                .map(HttpResponseCollect::getBody)
                .ifError("登录8196服务器主页失败")
                .toGetOrElseThrow(RuntimeException::new);
    }

    /**
     * 登录订单页面
     * @return
     */
    public String toConfirmOrderPage(){
        return HttpRequestUtil.postWithMultipleInfo("https://202.100.182.103:8196/Pages/PurchaseOrderList/ConfirmOrder.aspx","GET",new HashMap<>(), this::setCookie)
                .map(HttpResponseCollect::getBody)
                .ifError("登录8196服务器主页失败")
                .toGetOrElseThrow(RuntimeException::new);
    }

    //序列化表单
    public Map<String,String> serializeForm(String page){
        Document parse = Jsoup.parse(page);
        HashMap<String, String> map = new HashMap<>();
        parse.select("#queryForm input").forEach(element -> {
            String value = element.attr("value");
            if(StringUtils.isNotEmpty(value))
                map.put(element.attr("name"),element.attr("value"));
        });
        return map;
    }

    public String getConfirmOrderList(){

        Map<String, String> serializeForm = serializeForm(toConfirmOrderPage());

        String api = "https://202.100.182.103:8196/HSNN/CM/Trade/Web/Controller/PurchaseOrderListController/QueryOrderByComPS.HSNN";

        Map<String, String> formMap = new HashMap<>();

        formMap.put("_search","false");
        formMap.put("nd",String.valueOf(new Date().getTime()));
        formMap.put("rows","10");
        formMap.put("page","1");
        formMap.put("sidx","");
        formMap.put("sord","asc");

        return HttpRequestUtil.postUrlEncode(api,serializeForm,formMap,this::setCookie);
    }


    public String queryData(){
        String queryApi = "https://202.100.182.103:8196/HSNN/CM/Trade/Web/Controller/PurchaseOrderListController/QueryOrderByComPS.HSNN?__VIEWSTATE=k4%2BKe2%2BFLoGksG5xsdElvfCJESSgdyxzBTSRDxoVTv9q7%2FsEw0EJuzjDtIVZiVvXflippbaeStzFhDNuOEImL%2FuIgfLPEOvuqb9Bk5G0nZu40Ww6XH9aDQGLW1A%3D&__VIEWSTATEGENERATOR=49167D76&__EVENTVALIDATION=nQcNlS3a0yox3Eoe7i1uJlZ994S5Uo9c76GhkzCQD6yWIIbnYWEB9HBiNB18cQhKZdvXrehJgCHC1CEaTItmlJqIIuE%2B%2FpkKHv0ZBnAj5T3%2BzbZKvjPlQpQh92t0Wa6pFZfrjwJHHirmAMTE&ctl00%24ContentPlaceHolder%24MyHiddenFileId=AAdsdsdsd22331&PURCHASEORDERCODE=&PURCHASEORDERNAME=&HOSNAME=&SUBMITTIMEST=&SUBMITTIMEED=";
        return HttpRequestUtil.postWithMultipleInfo(queryApi,"POST",new HashMap<>(), httpRequest -> {

                    setCookie(httpRequest);

                    httpRequest.header("Accept","application/json, text/javascript, */*; q=0.01");
            //httpRequest.header("Accept-Encoding","gzip, deflate");
                    httpRequest.header("Accept-Language","zh-Hans-CN, zh-Hans; q=0.5");
                    httpRequest.header("Cache-Control","no-cache");
                    httpRequest.header("Connection","Keep-Alive");
                   // httpRequest.header("Content-Length","60");
                    httpRequest.header("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
                    httpRequest.header("Host","202.100.182.103:8196");
                    httpRequest.header("Referer","https://202.100.182.103:8196/Pages/PurchaseOrderList/PurchaseOrderConfirm.aspx");
                    httpRequest.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko");
                    httpRequest.header("X-Requested-With","XMLHttpRequest");

                })
                .map(HttpResponseCollect::getBody)
                .ifError("调用数据失败")
                .toGetOrElseThrow(RuntimeException::new);
    }


}
